package hu.issue.framework.web.validation;

import javax.servlet.http.HttpServletRequest;

public class NotEqualValidator implements Validator {
	private final String field1;
	private final String field2;
	private final String errorMessage;

	public NotEqualValidator(final String field1, final String field2, final String errorMessage) {
		this.field1 = field1;
		this.field2 = field2;
		this.errorMessage = errorMessage;
	}

	@Override
	public boolean validate(final HttpServletRequest request, final ErrorMessages messages) {
		final String field1Value = request.getParameter(this.field1);
		final String field2Value = request.getParameter(this.field2);

		if ((field1Value != null && field2Value != null && field1Value.equals(field2Value)) || (field1Value == null || field2Value == null)) {
			return true;
		}

		messages.addFieldMessage(this.field1, this.errorMessage);
		return false;
	}
}
